////////////////////////////////////////////////////旅游足迹////////////////////////////////////
const config = require('../config');

module.exports = function (app, query, jwt) {

  //根据目录获取文章
  app.get('/footprintlist', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.user);
    console.log(req.query);
    const { userId } = req.user;
    const { srcId, searchtext } = req.query;
    if (srcId) {
      let sql = 'select * from footprint where user_id=? and  footprintsrc_id=?';
      if (!!searchtext) {
        sql += ' and title like "%' + searchtext + '%"';
      }
      //获取文章
      query(sql, [userId, srcId], function (err, rows, field) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          let data = [];
          for (row of rows) {
            data.push({
              brief: row.brief,
              id: row.id,   //id
              title: row.title,    //文章标题
              footprintsrcId: row.footprintsrc_id, //所属目录Id
              createBy: row.create_by,   //创建人
              createTime: row.create_time,   //创建时间
              updateBy: row.update_by,   //修改人
              updateTime: row.update_time,   //修改时间
            });
          }
          const json = {
            code: true,
            message: "文章列表获取成功！",
            data
          };
          console.log(json);
          res.json(json);
        }
      });
    } else {
      const json = {
        code: false,
        message: "缺少目录id"
      };
      console.log(json);
      res.json(json);
    }
  });

  app.get('/footprint', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.user);
    console.log(req.query);
    const { fid } = req.query;
    let sql = 'select * from footprint where id=?';
    //获取文章
    query(sql, [fid], function (err, rows, field) {
      if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows && rows.length > 0) {
          const data = {
            id: rows[0].id,   //id
            title: rows[0].title,    //文章标题
            brief: rows[0].brief,    //文章简介
            footprint: rows[0].footprint,   //文章内容
            footprintsrcId: rows[0].footprintsrc_id, //所属目录Id
            createBy: rows[0].create_by,   //创建人
            createTime: rows[0].create_time,   //创建时间
            updateBy: rows[0].update_by,   //修改人
            updateTime: rows[0].update_time,   //修改时间
          }
          const json = {
            code: true,
            message: "文章列表获取成功！",
            data
          };
          console.log(json);
          res.json(json);
        }
      }
    });
  });


  //新增文章
  app.post('/footprint', jwt({ secret: config.secret }), function (req, res, next) {
    let { title, brief, text, srcId } = req.body;
    const { userId, username } = req.user;
    const nowDate = new Date();
    if (!brief) {
      brief = String(text).substring(0, 20);
    }
    const footprint = {
      title,
      brief,
      footprint: text,
      footprintsrc_id: srcId,
      user_id: userId,
      create_by: username,
      create_time: nowDate,
      update_by: username,
      update_time: nowDate
    };
    query('insert into footprint set ?', footprint, function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "新增成功"
        };
        console.log(json);
        res.json(json);
      }
    });
  });

  //修改文章
  app.put('/footprint', jwt({ secret: config.secret }), function (req, res, next) {
    const params = [
      req.body.title,
      req.body.footprint,
      req.body.footprintsrcId,
      req.user.userId,
      req.user.username,
      new Date(),
      req.body.id
    ];

    query('update footprint set title=?,footprint=?,footprintsrc_id=?,user_id=?,update_by=?,update_time=? where id=?', params, function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "修改成功"
        };
        console.log(json);
        res.json(json);
      }
    })
  });

  //删除文章
  app.delete('/footprint', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.query.id);
    const { userId } = req.user;
    const { id } = req.query;
    query('delete from footprint where id=? and user_id=?', [id, userId], function (error, data, field) {
      if (error) {
        const json = {
          code: false,
          message: error.message
        };
        console.log(json);
        res.json(json);
      } else {
        const json = {
          code: true,
          message: "删除成功"
        };
        console.log(json);
        res.json(json);
      }
    })
  });
};

